package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.EnterpriseList;
import com.isunland.managesystem.entity.ShouldGgatherPay;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.Date;

/**
 * 应收、付款查询
 *
 * @author wu
 */
public class ShouldGatherPaySearchFragment extends Fragment
    implements OnClickListener, OnCheckedChangeListener {
  private static final int REQUEST_START_TIME = 0;
  private static final int REQUEST_END_TIME = 1;
  private static final int REQUEST_UNIT = 2;
  public static final String EXTRA_TYPE =
      "com.isunland.managesystem.ui.ShouldGatherPaySearchFragment.EXTRA_TYPE";
  public static final String EXTRA_GATHER =
      "com.isunland.managesystem.ui.ShouldGatherPaySearchFragment.EXTRA_GATHER";
  public static final String EXTRA_PAY =
      "com.isunland.managesystem.ui.ShouldGatherPaySearchFragment.EXTRA_PAY";
  public static final String EXTRA_QUERY_VALUE =
      "com.isunland.managesystem.ui.ShouldGatherPaySearchFragment.EXTRA_QUERY_VALUE";
  public static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.ShouldGatherPaySearchFragment.EXTRA_VALUE";
  private TextView mtvUnitGatherPay;// 往来单位
  private TextView mtvStartDateGatherPay;// 开始时间
  private TextView mtvEndDateGatherPay;// 结束时间
  private TextView metContractCode;// 合同订单
  private CheckBox mcbNotPayGatherPay;// 未支付
  private CheckBox mcbPartPayGatherPay;// 部分支付
  private CheckBox mcbHasPayGatherPay;// 已支付

  private ShouldGgatherPay mQuery = new ShouldGgatherPay();
  private String mType;

  @Override public void onCreate(Bundle savedInstanceState) {
    // 设置返回图标
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mQuery = (ShouldGgatherPay) getArguments().getSerializable(EXTRA_VALUE);
    mType = getArguments().getString(EXTRA_TYPE);
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_gather_pay, container, false);
    mtvUnitGatherPay = (TextView) view.findViewById(R.id.tv_unit_gatherPay);
    mtvStartDateGatherPay = (TextView) view.findViewById(R.id.tv_startDate_gatherPay);
    mtvEndDateGatherPay = (TextView) view.findViewById(R.id.tv_endDate_gatherPay);
    metContractCode = (TextView) view.findViewById(R.id.et_contractCode);

    mcbHasPayGatherPay = (CheckBox) view.findViewById(R.id.cb_hasPay_gatherPay);
    mcbNotPayGatherPay = (CheckBox) view.findViewById(R.id.cb_notPay_gatherPay);
    mcbPartPayGatherPay = (CheckBox) view.findViewById(R.id.cb_partPay_gatherPay);

    mtvStartDateGatherPay.setText(mQuery.getStratTime());
    mtvEndDateGatherPay.setText(mQuery.getEndTime());
    mtvUnitGatherPay.setText(mQuery.getPartName());
    mtvStartDateGatherPay.setText(mQuery.getStratTime());
    mtvEndDateGatherPay.setText(mQuery.getEndTime());
    metContractCode.setText(mQuery.getContractCode());

    if (!TextUtils.isEmpty(mQuery.getHaspay())) {
      mcbHasPayGatherPay.setChecked(true);
    } else {
      mcbHasPayGatherPay.setChecked(false);
    }
    if (!TextUtils.isEmpty(mQuery.getNotPay())) {
      mcbNotPayGatherPay.setChecked(true);
    } else {
      mcbNotPayGatherPay.setChecked(false);
    }
    if (!TextUtils.isEmpty(mQuery.getPartPay())) {
      mcbPartPayGatherPay.setChecked(true);
    } else {
      mcbPartPayGatherPay.setChecked(false);
    }

    if (EXTRA_PAY.equalsIgnoreCase(mType)) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.shouldPayQuery);
      mcbHasPayGatherPay.setText(R.string.hasPay);
      mcbNotPayGatherPay.setText(R.string.notPay);
      mcbPartPayGatherPay.setText(R.string.partPay);
    } else if (EXTRA_GATHER.equalsIgnoreCase(mType)) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar()
          .setTitle(R.string.shouldGatherQuery);
      mcbHasPayGatherPay.setText(R.string.hasCalculate);
      mcbNotPayGatherPay.setText(R.string.notCalculate);
      mcbPartPayGatherPay.setText(R.string.partCalculate);
    }
    mtvUnitGatherPay.setOnClickListener(this);
    mtvStartDateGatherPay.setOnClickListener(this);
    mtvEndDateGatherPay.setOnClickListener(this);

    mcbHasPayGatherPay.setOnCheckedChangeListener(this);
    mcbNotPayGatherPay.setOnCheckedChangeListener(this);
    mcbPartPayGatherPay.setOnCheckedChangeListener(this);

    return view;
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      // 往来单位
      case R.id.tv_unit_gatherPay:
        Intent intent = new Intent(getActivity(), EnterprisePagerActivity.class);
        startActivityForResult(intent, REQUEST_UNIT);
        break;
      // 开始时间
      case R.id.tv_startDate_gatherPay:
        showDialog(REQUEST_START_TIME, new Date());
        break;
      // 结束时间
      case R.id.tv_endDate_gatherPay:
        showDialog(REQUEST_END_TIME, new Date());
        break;
      default:
        break;
    }
  }

  @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch (buttonView.getId()) {
      // 已支付
      case R.id.cb_hasPay_gatherPay:
        if (isChecked) {
          mQuery.setHaspay("T");
        } else {
          mQuery.setHaspay("");
        }
        break;
      // 未支付
      case R.id.cb_notPay_gatherPay:
        if (isChecked) {
          mQuery.setNotPay("T");
        } else {
          mQuery.setNotPay("");
        }
        break;
      // 部分支付
      case R.id.cb_partPay_gatherPay:
        if (isChecked) {
          mQuery.setPartPay("T");
        } else {
          mQuery.setPartPay("");
        }
        break;

      default:
        break;
    }
  }

  private void showDialog(int requestCode, Date date) {
    DialogFragment dialog = null;

    switch (requestCode) {
      case REQUEST_START_TIME:
        //			dialog = WorkProgressTimeDialogFragment.newInstance(date, R.string.choseEndDate);
        dialog = new BaseYMDTimeDialogFragment();

        break;
      case REQUEST_END_TIME:
        //			dialog = BaseTimeDialogFragment.newInstance(date, R.string.choseEndDate);
        dialog = new BaseYMDTimeDialogFragment();
        break;
      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(ShouldGatherPaySearchFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_START_TIME && data != null) {
      //			Date acquireDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      //			String startTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      //			mQuery.setStratTime(startTime);
      //			mtvStartDateGatherPay.setText(mQuery.getStratTime());
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      String startTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      mQuery.setStratTime(startTime);
      mtvStartDateGatherPay.setText(mQuery.getStratTime());
    }

    if (requestCode == REQUEST_END_TIME && data != null) {
      //			Date acquireDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      //			String endTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      //			mQuery.setEndTime(endTime);
      //			mtvEndDateGatherPay.setText(mQuery.getEndTime());
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      String endTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      mQuery.setEndTime(endTime);
      mtvEndDateGatherPay.setText(mQuery.getEndTime());
      return;
    }

    if (requestCode == REQUEST_UNIT) {
      EnterpriseList item =
          (EnterpriseList) data.getSerializableExtra(EnterpriseListFragment.EXTRA_ENTERPRISE);
      mQuery.setPartId(item.getId());
      mQuery.setPartName(item.getEnterpriseName());
      mtvUnitGatherPay.setText(mQuery.getPartName());
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }

      case R.id.menu_item_confirm:
        Intent intent = new Intent();
        mQuery.setContractCode(metContractCode.getText().toString());
        if (TextUtils.isEmpty(mQuery.getHaspay())
            && TextUtils.isEmpty(mQuery.getNotPay())
            && TextUtils.isEmpty(mQuery.getPartPay())) {
          ToastUtil.show(R.string.atLeastChooseOne);
          return false;
        }
        intent.putExtra(EXTRA_QUERY_VALUE, mQuery);
        getActivity().setResult(Activity.RESULT_OK, intent);
        getActivity().finish();

        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  public static Fragment newInstance(String type, ShouldGgatherPay query) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_TYPE, type);
    bundle.putSerializable(EXTRA_VALUE, query);
    ShouldGatherPaySearchFragment fragment = new ShouldGatherPaySearchFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
